<?php

namespace App\Http\Controllers\Wechat;

use App\Http\Controllers\Controller;
use App\Services\FleaMsgService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class FleaMsgController extends Controller
{

    protected $service;

    public function __construct(FleaMsgService $fleaMsgService)
    {
        $this->service = $fleaMsgService;
    }


    /**
     * @Notes 添加留言
     * @param Request $request
     * @return array
     * @author ypp
     * @date 2021/3/9
     * @time 11:34
     */
    public function add_msg(Request $request): array
    {
        $data = $request->post();
        //数据验证
        $validator = Validator::make($data, [
            'goods_id'    => 'required',
            'content'    => 'required',
        ], [
            'goods_id.required'      => '商品参数错误',
            'content.required'      => '请填写留言内容',
        ]);

        if ($validator->fails()) {
            $errors = $validator->errors();
            return $this->error($errors->first());
        }

        $info = $this->service->addFleaMsg($data);
        return $info['status'] ? $this->success([]) : $this->error(['msg']);
    }

    /**
     * @Notes 用户的闲置列表
     * @return array
     * @author ypp
     * @date 2021/3/6
     * @time 11:38
     */
    public function index(): array
    {
        $info = $this->service->getFealMsgListByUserId();
        return $info['status'] ? $this->success($info['data']) : $this->error(['msg']);
    }

}
